import Vue from 'vue'
import Router from 'vue-router'
import Progress from 'nprogress'
import Layout from '@/layout/layout'
import Login from '@/views/login/login'
import NotFound from '@/views/404'
import System from '@/views/system/index'
import {
  getToken
} from '@/common/utils/user'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '*',
      redirect: '/404',
      name: 'notFound',
      hidden: true
    },
    {
      path: '/',
      redirect: '/login'

    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/404',
      name: '404',
      component: NotFound
    },
    {
      path: '/about',
      component: Layout,
      children: [{
        path: 'home',
        name: 'home',
        component: () => import('@/views/home/index'),
        meta: {
          requireAuth: true
        }
      }]
    },{
      path: '/design',
      component: Layout,
      hidden: true,
      children: [ {
        path: 'trans',
        name: 'Trans',
        component: () => import('@/views/trans/index'),
        meta: {
          requireAuth: true
        }
      },{
        path: 'transFlow',
        name: 'TransFlow',
        component: () => import('@/views/trans/right/graph/index'),
        meta: {
          requireAuth: true
        }
      },]
    },
    {
      path: '/system',
      component: Layout,
      hidden: true,
      children: [{
        path: '/',
        component: () =>
          import('@/views/system/index'),
        children: [{
          path: 'database',
          name: 'Database',
          component: () =>
            import('@/views/system/database/index'),
          meta: {
            requireAuth: true
          }
        },{
          path: 'slave',
          name: 'Slave',
          component: () =>
            import('@/views/system/slave/index'),
          meta: {
            requireAuth: true
          }
        },{
          path: 'cluster',
          name: 'Cluster',
          component: () =>
            import('@/views/system/cluster/index'),
          meta: {
            requireAuth: true
          }
        }]
      }]
    }]
})

router.beforeEach((to, from, next) => {
  Progress.start();
  if (to.path === '/' || to.path === "/login") {
    next();
  } else {
    if (to.matched.some(record => record.meta.requireAuth)) {
      let auth_token = getToken();
      if (auth_token) {
        next();
      } else {
        next({
          path: "/login"
        });
      }
    }
  }
  next();
});
router.afterEach(() => {
  Progress.done()
})

export default router
